﻿using Autofac.Extensions.DependencyInjection;
using Common.Framework.Core.Extension;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
using Serilog.Formatting.Compact;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Framework.Core.Foundation
{
    /// <summary>
    /// 
    /// </summary>
    public static class ProgramFoundation
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        public static void Main<TStartup>(string[] args) where TStartup:class
        {
            Serilog.SerilogExtension.InitLog();
            try
            {
                Log.Information("Starting up");
                CreateHostBuilder<TStartup>(args).Build().Run();
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Application start-up failed");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TStartup"></typeparam>
        /// <param name="args"></param>
        /// <returns></returns>
        public static IHostBuilder CreateHostBuilder<TStartup>(string[] args) where TStartup : class
        {
            return Host.CreateDefaultBuilder(args)
                 .UseServiceProviderFactory(new AutofacServiceProviderFactory()) //将默认ServiceProviderFactory指定为AutofacServiceProviderFactory
                 .UseSerilog()
                 .ConfigureWebHostDefaults(webBuilder =>
                 {
                     webBuilder.UseStartup<TStartup>();
                 });
        }

        //public static void InitLog()
        //{ 
        //    Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(ConfigurationExtension.Configuration)
        //    .Enrich.FromLogContext()
        //    .WriteTo.Console() //输出到控制台上的日志为正常格式,方便查看
        //    .MinimumLevel.Debug() // 所有Sink的最小记录级别
        //    //.WriteTo.File(new CompactJsonFormatter(), path: Path.Combine("logs\\verbose", "log-verbose-.txt"), rollingInterval: RollingInterval.Day,restrictedToMinimumLevel:LogEventLevel.Verbose)
        //    //.WriteTo.File(new CompactJsonFormatter(), path: Path.Combine("logs\\debug", "log-debug-.txt"), rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Debug)
        //    .WriteTo.File(new CompactJsonFormatter(), path: Path.Combine("logs\\information", "log-information-.txt"), rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Information)
        //    .WriteTo.File(new CompactJsonFormatter(), path: Path.Combine("logs\\warning", "log-warning-.txt"), rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Warning)
        //    .WriteTo.File(new CompactJsonFormatter(), path: Path.Combine("logs\\error", "log-error-.txt"), rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Error)
        //    .WriteTo.File(new CompactJsonFormatter(), path: Path.Combine("logs\\fatal", "log-fatal-.txt"), rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Fatal)
        //    //直接将information写入elk
        //    //.WriteTo.Elasticsearch(new Serilog.Sinks.Elasticsearch.ElasticsearchSinkOptions(new Uri("http://127.0.0.1:9200"))
        //    //
        //    //    MinimumLogEventLevel = LogEventLevel.Information,
        //    //    IndexFormat = $"micro-notice-test-stash-{DateTime.Now:yyyy.MM.dd}"
        //    //})
        //    .CreateLogger();
        //}

        ///// <summary>
        ///// 
        ///// </summary>
        //private static IConfiguration Configuration { get; } = new ConfigurationBuilder()
        //    .SetBasePath(Directory.GetCurrentDirectory())
        //    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        //    .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
        //    .AddEnvironmentVariables()
        //    .Build();
    }
}
